import {
    RouterMount,
    createRouter
} from 'uni-simple-router';

const router = createRouter({
    platform: process.env.VUE_APP_PLATFORM,
    routes: [...ROUTES, {
        path: '*',
        redirect: (to) => {
            return {
                name: 'notfound'
            } // 匹配非定义的路由地址
        }
    }]
});
//全局路由前置守卫
router.beforeEach((to, from, next) => {
    // console.log('to===========>', to);
    // console.log('from==========>', from);
    // #ifndef H5
    const isLogin = uni.getStorageSync('isLogin');
    // #endif
    // #ifdef H5
    const isLogin = sessionStorage.getItem('isLogin');
    // #endif
    if (to.path != '/pages/number/login' && to.path != '/pages/number/register') {
        if (!isLogin) {
            next({
                path: '/pages/number/login'
            })
        }
    }
    if (to.path == '/pages/number/login' && to.path == '/pages/number/login') {
        if (isLogin) {
            // #ifdef H5
            next({
                path: '/home'
            })
            // #endif
            // #ifndef H5
            next({
                path: '/pages/home/home'
            })
            // #endif
        }
    }
    if (to.path == '/logout') {
        // #ifndef H5
        uni.clearStorageSync();
        // #endif
        // #ifdef H5
        sessionStorage.clear()
        // #endif
        // 跳转到登录页面
        next({
            path: '/pages/number/login'
        })
    }
    next()
});
// 全局路由后置守卫
router.afterEach((to, from) => {
    // console.log('跳转结束')
})

export {
    router,
    RouterMount
}